﻿using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Domain.Impl
{
    public class AppearanceModelRepository : IAppearanceModelRepository
    {


        /// <summary>
        /// 上下文
        /// </summary>

        private readonly AgricultureDbContext context;
        public AppearanceModelRepository(AgricultureDbContext context)
        {
            this.context = context;
        }

       
        /// <summary>
        /// 获取所有品相测定数据
        /// </summary>
        /// <returns></returns>
        public async Task<IQueryable<Appearancemodel>> Appearance()
        {
            return context.Appearancemodels;
        }

        /// <summary>
        /// 获取品相测定数据列表
        /// </summary>
        /// <param name="earTag"></param>
        /// <param name="variety"></param>
        /// <param name="sheepRescissionSex"></param>
        /// <returns></returns>
        public async Task<List<Appearancemodel>> AppearanceModelList(string earTag, int variety, bool sheepRescissionSex)
        {
            var departs = context.Appearancemodels.Where(c=>c.LogicalDeletion==0).AsQueryable().AsNoTracking();
            if (!string.IsNullOrEmpty(earTag))
            {
                departs = departs.Where(c => c.EarTag.Contains(earTag));
            }

            if (variety > 0)
            {
                departs = departs.Where(c => c.Variety == variety);
            }
            if (sheepRescissionSex)
            {
                departs = departs.Where(c => c.SheepRescissionSex == sheepRescissionSex);
            }

            return departs.ToList();
        }


        /// <summary>
        /// 删除品相测定数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> Delete(params int[] ids)
        {
            var sheeps = context.Appearancemodels.Where(c => ids.Contains(c.AppearanceId));
            foreach (var item in sheeps)
            {
                item.LogicalDeletion = 1;
            }
            return await context.SaveChangesAsync();

        }


        /// <summary>
        /// 新增品相测定数据
        /// </summary>
        /// <param name="appearancemodel"></param>
        /// <returns></returns>
        public async Task<int> Add(Appearancemodel appearancemodel)
        {
            context.Appearancemodels.Add(appearancemodel);
            return await context.SaveChangesAsync();
        }




        /// <summary>
        /// 更新品相测定数据
        /// </summary>
        /// <param name="appearancemodel"></param>
        /// <returns></returns>
        public async Task<int> Update(Appearancemodel appearancemodel)
        {
            context.Appearancemodels.Update(appearancemodel);
            return await context.SaveChangesAsync();
        }







    }




}
